package com.boe.csb.web.converter;

import com.boe.csb.core.common.exception.CSBException;
import com.boe.csb.core.common.exception.CSBErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;

import java.util.Date;

/**
 * Date: 2014/12/15 15:39
 *
 * @author tianjin.lp <a href="mailto:liupengjava@gmail.com">Ricoul</a>
 */
public class StringToDateConverter implements Converter<String, Date> {

    private static final Logger LOGGER = LoggerFactory.getLogger(StringToDateConverter.class);

    @Override
    public Date convert(String source) {
        Date date;
        try {
            date = new Date(Long.valueOf(source));
        } catch (Exception e) {
            String errMsg = "string2date类型转换错误,errStr=" + source;
            LOGGER.error(errMsg, e);
            throw new CSBException(CSBErrorCode.UNKNOWN_ERROR, errMsg, e);
        }
        return date;
    }
}